// component/wx-index-list/wx-index-list.js
var common = require("../../utils/util.js");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    data: {
      type: Object,
      value: {},
    },
    myCity: {
      type: String,
      value: "",
    },
    // 用于外部组件搜索使用
    search:{
      type:String,
      value:"",
      observer: function (newVal, oldVal) { 
        console.log(newVal)
        this.value = newVal;
        this.searchMt();
      }
    }
  },

  data: {
    list: [],
    rightArr: [],// 右侧字母展示
    jumpNum: '',//跳转到那个字母
    myCityName: '请选择' // 默认我的城市

  },
  ready() {
    let data = this.data.data;
    this.resetRight(data);
    if (this.data.myCity) {
      this.getCity()
    }
  },
  methods: {
    // 数据重新渲染
    resetRight(data) {
      let rightArr = []
      for (let i in data) {
        rightArr.push(data[i].title.substr(0, 1));
      }
      this.setData({
        list: data,
        rightArr
      })
    },
    getCity() {
        var pages = getCurrentPages();
        var currPage = pages[pages.length - 1];
        var prevPage = pages[pages.length - 2];
        getApp().globalData.latitude = getApp().globalData.lat;
        getApp().globalData.longitude = getApp().globalData.lng;
        prevPage.setData({
            address: getApp().globalData.address
        });
        wx.navigateBack({})
    },
    // 右侧字母点击事件
    jumpMt(e) {
      let jumpNum = e.currentTarget.dataset.id;
      this.setData({ jumpNum });
    },
    // 列表点击事件
    detailMt(e) {
      let detail = e.currentTarget.dataset.detail;
      
      let myEventOption = {
        bubbles: false,//事件是否冒泡
        composed: false,//事件是否可以穿越组件边界
        capturePhase: false //事件是否拥有捕获阶段
      }; // 触发事件的选项
      // this.triggerEvent('detail', detail, myEventOption)
        var that = this;
        common.ajax({
            url: 'User/getLngAndLat',
            data: {
              address: detail.name
            },
            loading: '加载中...',
            userinfo: true,
            success: function (res) {
                if (res.status == 'SUCCESS') {
                    // res.result.list.result.location.lng;
                    var pages = getCurrentPages();
                    var currPage = pages[pages.length - 1];
                    var prevPage = pages[pages.length - 2];
                    getApp().globalData.latitude = res.result.list.result.location.lat;
                    getApp().globalData.longitude = res.result.list.result.location.lng;
                    prevPage.setData({
                        address: detail.name
                    });
                    wx.navigateBack({})
                } else {
                    common.info(res.result.msg)
                }
            }
        });
    },
    // 获取搜索输入内容
    input(e) {
      this.value = e.detail.value;
        this._search();
    },
    // 基础搜索功能
    searchMt() {
      this._search();
    },
    _search(){
      let data = this.data.data;
      let newData = [];
      for (let i = 0; i < data.length; i++) {
        let itemArr = [];
        for (let j = 0; j < data[i].item.length; j++) {
          if (data[i].item[j].name.indexOf(this.value) > -1) {
            let itemJson = {};
            for (let k in data[i].item[j]) {
              itemJson[k] = data[i].item[j][k];
            }
            itemArr.push(itemJson);
          }
        }
        if (itemArr.length === 0) {
          continue;
        }
        newData.push({
          title: data[i].title,
          type: data[i].type ? data[i].type : "",
          item: itemArr
        })
      }
      this.resetRight(newData);
    },
    // 城市定位
    locationMt() {
      // TODO 暂时没有实现 定位自己的城市，需要引入第三方api
    }

  }
})
